
/**
 * @apiDefine ErrorResponse
 * @apiError (请求错误) {String} err_message 错误信息描述
 */

/**
 * @apiVersion 1.0.0
 * @apiGroup  轮播图
 * @api {POST} api/banner/create 新增轮播图
 * @apiName api/banner/create
 * @apiExample {js} axios 例子:
 *  axios.post('http://39.105.38.34:8080/api/banner/create', {
 *    img: '123.jpg',
 *    link: 'www.baidu.com',
 *    storeid: 1,
 *  });
 * @apiHeader {String} authorization 用户登录或注册成功时返回的 token
 * @apiParam {String} img 图片名字
 * @apiParam {String} link 图片要跳转的链接
 * @apiParam {Number} storeid 店铺的ID
 * @apiSuccess (请求成功) {Object} banner 轮播图数据
 * @apiuse  ErrorResponse
 */

const { query, create } = require('../../helper/db');
const { ok, door } = require('../../helper/util');

module.exports = async ctx => {
  var token = ctx.headers.authorization;
  var params = ctx.request.body;

  await door(token);
  var bannerlist = await query('select * from banner where storeid = ?',[params.storeid]);

  if (bannerlist.length > 10) {
    throw new Error('轮播图不能大于10张');
  }

  var store = await create('banner', {
    img: params.img,
    link: params.link,
    storeid: params.storeid,
  })

  ctx.body = ok({store});
}
